defmodule ProgContest.DockerImages do
  @moduledoc """
  The DockerImages context.
  """

  import Ecto.Query, warn: false
  alias ProgContest.Repo

  alias ProgContest.DockerImages.DockerImage

  @doc """
  Returns the list of docker_images.

  ## Examples

      iex> list_docker_images()
      [%DockerImage{}, ...]

  """
  def list_docker_images do
    Repo.all(DockerImage)
  end

  def find_docker_image(quiz_id, language) do
    Repo.one from image in DockerImage,
             where: image.quiz_id == ^quiz_id and image.language == ^language
  end

  @doc """
  Gets a single docker_image.

  Raises `Ecto.NoResultsError` if the Docker image does not exist.

  ## Examples

      iex> get_docker_image!(123)
      %DockerImage{}

      iex> get_docker_image!(456)
      ** (Ecto.NoResultsError)

  """
  def get_docker_image!(id), do: Repo.get!(DockerImage, id)

  @doc """
  Creates a docker_image.

  ## Examples

      iex> create_docker_image(%{field: value})
      {:ok, %DockerImage{}}

      iex> create_docker_image(%{field: bad_value})
      {:error, %Ecto.Changeset{}}

  """
  def create_docker_image(attrs \\ %{}) do
    %DockerImage{}
    |> DockerImage.changeset(attrs)
    |> Repo.insert()
  end

  @doc """
  Updates a docker_image.

  ## Examples

      iex> update_docker_image(docker_image, %{field: new_value})
      {:ok, %DockerImage{}}

      iex> update_docker_image(docker_image, %{field: bad_value})
      {:error, %Ecto.Changeset{}}

  """
  def update_docker_image(%DockerImage{} = docker_image, attrs) do
    docker_image
    |> DockerImage.changeset(attrs)
    |> Repo.update()
  end

  @doc """
  Deletes a DockerImage.

  ## Examples

      iex> delete_docker_image(docker_image)
      {:ok, %DockerImage{}}

      iex> delete_docker_image(docker_image)
      {:error, %Ecto.Changeset{}}

  """
  def delete_docker_image(%DockerImage{} = docker_image) do
    Repo.delete(docker_image)
  end

  @doc """
  Returns an `%Ecto.Changeset{}` for tracking docker_image changes.

  ## Examples

      iex> change_docker_image(docker_image)
      %Ecto.Changeset{source: %DockerImage{}}

  """
  def change_docker_image(%DockerImage{} = docker_image) do
    DockerImage.changeset(docker_image, %{})
  end
end
